package net.learn2develop.GPS;

import java.util.List;

import net.learn2develop.DB.DatabaseHelper;
import net.learn2develop.view.MyItemizedOverlay;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class AllFavorites extends MapActivity{

	protected int placeId; 
    private MapView mapView;
	private MapController mc;
	private GeoPoint p;
	private Drawable[] markers;
	private List<Overlay> mapOverlays;
	private String attributes [] = {"nameplace" , "latitud", "longitud", "address", "phone", "url", "email", "icon", "photo", "promotion", "_id"};
	private String[] infoPlace;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_search);
        
        mapView = (MapView) findViewById(R.id.mapView);
	    mc = mapView.getController();
        loadMarkers();
        
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        mapOverlays = mapView.getOverlays();
//        mapOverlays.clear();
        Cursor cursor = db.rawQuery("SELECT nameplace, address, latitud, longitud, phone, url, email, icon, photo, promotion, _id FROM places WHERE favorite LIKE 1",
        		new String[]{});

        if (cursor.moveToFirst()) {
		 	    do {
		 	    	infoPlace = new String[11];
		 	    	for (int item = 0; item < attributes.length; item++){
			      		//ubico info en cada variable
					    infoPlace[item] = cursor.getString(cursor.getColumnIndex(attributes[item]));	  
		        	}
		 	    	
		 	    	double lat = Double.parseDouble(cursor.getString(cursor.getColumnIndex("latitud")));
			        double lon = Double.parseDouble(cursor.getString(cursor.getColumnIndex("longitud")));
			        
		 	    	p = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
			        
			        mc.animateTo(p);
			        mc.setZoom(15);
			        
			        addMarker(mapOverlays,0,infoPlace);
		 	    	
		 	       } while (cursor.moveToNext());
		 }
		 	
      	 if (cursor != null && !cursor.isClosed()) {
		 	         cursor.close();
		 }
        	
	}
	
    /**
 	 * Add a marker and a balloon for each place.
 	 * 
 	 * @param mapOverlays - list of overlays (including this) on the MapView.
 	 * @param idMarker - marker chosen.
 	 * @param infoPlace - contains info about place.
 	 */ 
     public void addMarker(List<Overlay> mapOverlays, int idMarker, String[] infoPlace){

    	String image = "<img src='"+infoPlace[8]+"'>"; 
       	MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(markers[idMarker], mapView, image, Integer.valueOf(infoPlace[10]));
		String info = " ";
    	for (int r = 3;r<7;r++){info = info+infoPlace[r]+" ";} 	
		OverlayItem overlayItem = new OverlayItem(p, infoPlace[0],info);
		itemizedOverlay.addOverlay(overlayItem);
		mapOverlays.add(itemizedOverlay);
		mapView.invalidate();
    }
     
    public void loadMarkers(){
     	
     	markers = new Drawable[11];
     	markers[0] = this.getResources().getDrawable(R.drawable.a);
     	markers[1] = this.getResources().getDrawable(R.drawable.b);
     	markers[2] = this.getResources().getDrawable(R.drawable.c);
     	markers[3] = this.getResources().getDrawable(R.drawable.d);
     	markers[4] = this.getResources().getDrawable(R.drawable.e);
     	markers[5] = this.getResources().getDrawable(R.drawable.f);
     	markers[6] = this.getResources().getDrawable(R.drawable.g);
     	markers[7] = this.getResources().getDrawable(R.drawable.h);
     	markers[8] = this.getResources().getDrawable(R.drawable.i);
     	markers[9] = this.getResources().getDrawable(R.drawable.j);
     	markers[10] = this.getResources().getDrawable(R.drawable.blue);
    } 
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}